﻿using System;
using System.Data;
using System.Data.Odbc;
using System.IO;
using System.Windows;

namespace csvformater.csvReader
{
	static class NzCsvLoader
    {
        public static DataSet LoadCsv(string csvFile)
        {
            string csvFileDir = Path.GetDirectoryName(csvFile);
            string fileNevCsv = Path.GetFileName(csvFile);
            var ds = new DataSet();
            try
            {
                // Creates and opens an ODBC connection
                string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + csvFileDir.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
                string sqlSelect;
                var conn = new OdbcConnection(strConnString.Trim());
                conn.Open();

                //Creates the select command text
         
         
                sqlSelect = "select * from [" + fileNevCsv.Trim() + "]";

                //Creates the data adapter
                var objOledbDa = new OdbcDataAdapter(sqlSelect, conn);

                //Fills dataset with the records from CSV file
                objOledbDa.Fill(ds, "csv");

                //closes the connection
                conn.Close();
            }
            catch (Exception e) //Error
            {
                MessageBox.Show(e.Message, "Error - LoadCSV");
            }
            return ds;
        }
        public static void WriteSchema(string filePath, string format, string characterSet)
        {
            var dirCsv = Path.GetDirectoryName(filePath);
            var fileNevCsv = Path.GetFileName(filePath);
            try
            {
                var fsOutput = new FileStream(dirCsv + "\\schema.ini", FileMode.Create, FileAccess.Write);
                var srOutput = new StreamWriter(fsOutput);
                string s1, s2, s3, s4, s5;

                s1 = "[" + fileNevCsv + "]";
                s2 = "ColNameHeader=true";
                s3 = string.Format("Format={0}", format);
                s4 = "MaxScanRows=25";
                s5 = string.Format("CharacterSet={0}",characterSet);

                srOutput.WriteLine(s1.ToString() + "\r\n" + s2.ToString() + "\r\n" + s3.ToString() + "\r\n" + s4.ToString() + "\r\n" + s5.ToString());
                srOutput.Close();
                fsOutput.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "writeSchema");
            }
        }
        private static void WriteSchema(string dirCsv, string fileNevCsv)
        {
            try
            {
                var fsOutput = new FileStream(dirCsv + "\\schema.ini", FileMode.Create, FileAccess.Write);
                var srOutput = new StreamWriter(fsOutput);
                string s1, s2, s3, s4, s5;

                s1 = "[" + fileNevCsv + "]";
                s2 = "ColNameHeader=true";
				s3 = "Format=TabDelimited";
                s4 = "MaxScanRows=25";
				s5 = "CharacterSet=ANSI";

                srOutput.WriteLine(s1.ToString() + "\r\n" + s2.ToString() + "\r\n" + s3.ToString() + "\r\n" + s4.ToString() + "\r\n" + s5.ToString());
                srOutput.Close();
                fsOutput.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "writeSchema");
            }
        }
    }
}
